package org.jeecg.modules.demo3.fly.service.impl.qun;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo3.fly.entity.AxhkWorkManagement;
import org.jeecg.modules.demo3.fly.entity.AxhkYwAgrplane;
import org.jeecg.modules.demo3.fly.entity.assist.AxhkLandManagements;
import org.jeecg.modules.demo3.fly.entity.assist.qun.*;
import org.jeecg.modules.demo3.fly.mapper.*;
import org.jeecg.modules.demo3.fly.service.qun.QunService;
import org.jeecg.modules.demo3.fly.utils.UuId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wjn
 * @version V1.0
 * @date 2021/6/30 14:15
 * @Copyright © 2021/6/30 深圳卓翼航空无人机有限公司
 */
@Service
public class QunServiceImpl extends ServiceImpl<AUserMapper, AUser> implements QunService {

    @Autowired
    private AUserMapper aUserMapper;
    @Autowired
    private FluckMapper fluckMapper;
    @Autowired
    private ThirdpartyMapper thirdpartyMapper;
    @Autowired
    private AFPartMapper afPartMapper;
    @Autowired
    private AxhkYwAgrplaneMapper axhkYwAgrplaneMapper;
    @Autowired
    private AxhkWorkManagementMapper axhkWorkManagementMapper;

    /*
     * 创建群
     * String number  创建用户的电话号 -->为队长电话号
     * 根据电话号查看 该用户是否已经存在于群中 如果存在 就不能够再建立群 并修改队长标识符
     * String name   群名
     * */
    @Override
    public String getCreatByFluck(String number, String fName) {
        Fluck fluck1 = null; // 根据群名查询出来的 群
        String msg = null;
        String fid = UuId.getUUID(); //添加的 id
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String creatTime = simpleDateFormat.format(new Date());//为获取当前系统时间
        if ((null != fName && "" != fName) && (null != number && "" != number)) { //当群名不为空时
            Integer uNum = 1;
            Fluck fluck = new Fluck(fid, fName, creatTime, uNum);
            fluck1 = fluckMapper.getByFluck(fName);
            if (null == fluck1) { //该群名 没有
                int i = fluckMapper.getByInsertFluck(fluck);
                // 当i>0 时 说明群组创建成功 此时应该查询出 用户 iD
                if (i > 0) {
                    String tid = UuId.getUUID();
                    AUser aUser = aUserMapper.getUserByPhone(number); //通过电话号码查询到的 用户
                    String uId = aUser.getId(); //获取用户id
                    Integer TAG = 1; //设置此用户的标识符 为队长
                    Thirdparty thirdparty = new Thirdparty(tid, uId, TAG, fid); //添加至群跟用户的关系表
                    int a = thirdpartyMapper.getByThirdparty(thirdparty);
                    if (a > 0) {
                        msg = fid;
                    }
                } else {
                    msg = "A";
                }
            } else { //该 群名 存在
                msg = "B";
            }
        } else { //当群名为空时
            msg = "C";
        }
        return msg;
    }

    /*
     * 添加 队员
     * String number 用户电话号码 查看有无该用户
     * String fId 群id 将用用户添加至此群
     * */
    @Override
    public String getInsertByTeam(String uId, String number, String fId) {
        String msg = null;
        if (null != uId && "" != uId) {
            Thirdparty thirdparty1 = thirdpartyMapper.getByUser(fId, uId); //判断该用户在该群中是否是队长的 存在
            Integer tag = thirdparty1.getTag();
            if (tag == 1) { //该用户是队长 有权限操作
                if ((null != number && "" != number) && (null != fId && "" != fId)) {
                    AUser aUser = aUserMapper.getUserByPhone(number); //通过电话号码查询到的 用户
                    if (null != aUser) { //存在该用户
                        String uIds = aUser.getId(); //用户id
                        Thirdparty user = thirdpartyMapper.getByUser(fId, uIds); //判断该用户是否已经存在于该群里
                        if (null == user) { //不存在此群
                            String tid = UuId.getUUID();
                            Integer TAG = 0; //设置此用户的标识符 为队员
                            Thirdparty thirdparty = new Thirdparty(tid, uIds, TAG, fId);
                            int a = thirdpartyMapper.getByThirdparty(thirdparty);
                            if (a > 0) {
                                Fluck fluck = fluckMapper.getFluck(fId);
                                Integer uNums = fluck.getUNum();
                                Integer uNum = uNums + 1;
                                fluckMapper.getUpdateFluck(fId, uNum);
                                msg = "用户添加成功！"; // 用户添加成功！
                            }
                        } else { //存在于此群
                            msg = "A"; //该用户已经存在于此群!
                        }
                    } else { // 不存在该用户
                        msg = "B"; //该用户未注册!
                    }
                } else {
                    msg = "C"; // 用户或者所要添加的群，不能为为空!
                }
            } else {
                msg = "D"; // 你不是队长没有权限添加队员!
            }
        }
        return msg;
    }

    /*
     * 移除队员
     * String uId 队长id
     * String fId 移除的群id
     * uId1 队员id
     * */
    @Override
    public String getDeleteByTeam(String uId1, String uId, String fId) {
        String msg = null;
        if (null != uId && "" != uId) {
            Thirdparty thirdparty1 = thirdpartyMapper.getByUser(fId, uId); //判断该用户在该群中是否是队长
            Integer tag = thirdparty1.getTag();
            if (tag == 1) {
                if ((null != uId1 && "" != uId1) && (null != fId && "" != fId)) { //用户存在 群存在
                    Thirdparty thirdparty = thirdpartyMapper.getByUser(fId, uId1); //判断该用户在该群中是否是队长
                    if (null != thirdparty) {
                        Integer TAG = thirdparty.getTag();
                        if (TAG != 1) { // 该用户不是该群的在、队长
                            int i = thirdpartyMapper.getByDeleteUser(fId, uId1);
                            if (i > 0) {
                                Fluck fluck = fluckMapper.getFluck(fId);
                                Integer uNums = fluck.getUNum();
                                Integer uNum = uNums - 1;
                                fluckMapper.getUpdateFluck(fId, uNum);
                                msg = "移除队员成功!";
                            }
                        } else { //该用户是该群的队长 不可删除
                            msg = "A"; //  不能移除，该用户是队长!
                        }
                    } else {
                        msg = "C"; // 该用户不在该群中！
                    }
                } else { // 用户不存在 群不存在
                    msg = "B"; // 该用户或者群不存在!
                }
            } else {
                msg = "D"; // 你不是队长没有权限移除队员!
            }
        }
        return msg;
    }

    /*
     * 转换队长
     * String uId 需要成为队长的用户
     * String fId 群id
     * */
    @Override
    public String getUpdateByTeam(String uId, String fId) {
        String msg = null;
        Thirdparty thirdparty = thirdpartyMapper.getByUser(fId, uId); //判断该用户在该群中 存在
        if (null != thirdparty) { // 该用户存在于该群
            Thirdparty thirdparty1 = thirdpartyMapper.getByThir(fId); //查询到 该群的队长 用户
            String uId1 = thirdparty1.getUId();
            if (uId.equals(uId1)) { //如果相同说明此用户已经是队长
                msg = "B"; //该用户已经是队长了！
            } else {
                Integer tag1 = 0;
                int i = thirdpartyMapper.getUpdateByTeam(fId, uId1, tag1);// 对该群的队长进行修改
                if (i > 0) {
                    Integer tag = 1;
                    int a = thirdpartyMapper.getUpdateByTeam(fId, uId, tag);
                    if (a > 0) {
                        msg = "转让队长成功!";
                    } else {
                        msg = "转让队长不成功!";
                    }
                }
            }
        } else { // 此用户不存在该群
            msg = "A"; //该用户不存在此群！
        }
        return msg;
    }

    /*
     * 队员自己退群
     * String uId 退群人id
     * String fId 群id
     * */
    @Override
    public String getExitFluck(String uId, String fId) {
        String msg = null;
        if (null != uId && null != fId) {
            Thirdparty thirdparty1 = thirdpartyMapper.getByUser(fId, uId); //判断该用户在该群中是否是队长
            Integer tag = thirdparty1.getTag();
            if (tag != 1) { //说明该退群用户不是队长
                int i = thirdpartyMapper.getByDeleteUser(fId, uId); //将该用户从关系表中删除
                if (i > 0) {
                    Fluck fluck = fluckMapper.getFluck(fId);
                    Integer uNums = fluck.getUNum();
                    Integer uNum = uNums - 1;
                    String fName = fluck.getFName();
                    int a = fluckMapper.getUpdateFluck(fId, uNum); //修改群成员数量
                    if (a > 0) {
                        List<AxhkYwAgrplane> axhkYwAgrplanes = axhkYwAgrplaneMapper.getSelectPlane(fName, uId);
                        if (axhkYwAgrplanes.size() != 0) {
                            String userName = "无";
                            String userId = null;
                            for (int n = 0; n < axhkYwAgrplanes.size(); n++) {
                                AxhkYwAgrplane axhkYwAgrplane = axhkYwAgrplanes.get(n);
                                int serialNumber = axhkYwAgrplane.getSerialNumber();
                                int conn_status = 1;
                                int a1 = axhkYwAgrplaneMapper.getUpdatePlane2(serialNumber, userName, userId, conn_status);
                                if (a1 > 0) {
                                    msg = "退群成功!";
                                }
                            }
                        } else {
                            msg = "B"; //退群成功!
                        }
                    }
                }
            } else { //该用户是队长 不可以退群
                msg = "A"; //队长不可以退群
            }
        }
        return msg;
    }

    /*
     * 添加设备
     * String fId 群id
     * int serialNumber 设备序列号
     * */
    @Override
    public String getAddByFacility(String fId, Integer serialNumber) {
        String msg = null;
        if ((null != fId && "" != fId) && (serialNumber != 0 && null != serialNumber)) {
            AFPart afPart = afPartMapper.getByAFPart(serialNumber);
            AxhkYwAgrplane allAxhkYwAgrplane = axhkYwAgrplaneMapper.getAllAxhkYwAgrplane(serialNumber);
            if (null == afPart && null != allAxhkYwAgrplane) { //该设备不在任何群中
                String id = UuId.getUUID();
                AFPart afPart1 = new AFPart(id, serialNumber, fId);
                int i = afPartMapper.getInsertAFPart(afPart1);//进行添加
                if (i > 0) { //存在该群中
                    Fluck fluck = fluckMapper.getFluck(fId);
                    String fName = fluck.getFName();
                    int a = axhkYwAgrplaneMapper.getUpdatePlane(serialNumber, fName);
                    if (a > 0) {
                        msg = "添加成功!"; //添加成功!
                    }
                }
            } else {
                msg = "A"; //该设备已经存在于群中!
            }
        } else {
            msg = "C"; //请正确选择设备!
        }
        return msg;
    }

    /*
     * 通过群将群中的设备给某个用户
     * String uId1 队长ID
     * String uId2 队员ID
     * int serialNumber  设备序列号
     * String fId 群ID
     * */
    @Override
    public String getAddUserOnFacility(String uId1, String uId2, Integer serialNumber, String fId) {
        String msg = null;
        AxhkYwAgrplane axhkYwAgrplane = null;
        if (0 != serialNumber && null != serialNumber) {
            axhkYwAgrplane = axhkYwAgrplaneMapper.getAllAxhkYwAgrplane(serialNumber);
            String userNames = axhkYwAgrplane.getUserName(); // 该设备下是否有用户
            if (userNames == "" || userNames.equals("无")) { //该设备下无用户
                if (null != uId2 && "" != uId2) {
                    Thirdparty thirdparty2 = thirdpartyMapper.getByUser(fId, uId2); //判断该用户在该群中
                    if (null != thirdparty2) { //在群中
                        AUser aUser = aUserMapper.getAllAUser(uId2);
                        String userName = aUser.getRealname();
                        int connStatus = 2;
                        int a = axhkYwAgrplaneMapper.getUpdatePlane2(serialNumber, userName, uId2, connStatus);
                        if (a > 0) {
                            msg = "配对成功!"; //添加成功!

                        }
                    } else { //不在群中
                        msg = "A"; // 此用户不在该群中!
                    }
                } else {
                    msg = "C"; // 请正确选择被添加人员
                }
            } else { //该设备下有用户
                if ((null != uId1 && "" != uId1) && (null != fId && "" != fId)) {
                    Thirdparty thirdparty1 = thirdpartyMapper.getByUser(fId, uId1); //判断该用户在该群中是否是队长
                    Integer tag = thirdparty1.getTag();
                    if (tag == 1) { // 是队长操作
                        if (null != uId2 && "" != uId2) {
                            Thirdparty thirdparty2 = thirdpartyMapper.getByUser(fId, uId2); //判断该用户在该群中
                            int astate = axhkYwAgrplane.getAstate();//手机App连接状态标识
                            if (null != thirdparty2 && astate == 0) { //在群中并且飞机状未连接时
                                AUser aUser = aUserMapper.getAllAUser(uId2);
                                String userName = aUser.getRealname();
                                int connStatus = 2;
                                int a = axhkYwAgrplaneMapper.getUpdatePlane2(serialNumber, userName, uId2, connStatus);
                                if (a > 0) {
                                    msg = "配对成功!"; //添加成功!
                                }
                            } else { //不在群中
                                msg = "A"; // 此用户不在该群中!
                            }
                        } else {
                            msg = "C"; // 请正确选择被添加人员
                        }
                    } else { //不是队长操作
                        msg = "B"; //你不是队长没有权限移除队员!
                    }
                }
            }
        } else {
            msg = "D"; // 请正确选择设备
        }
        return msg;
    }

    /*
     * 显示该群 所有设备
     * String fId 群ID
     * */
    @Override
    public List<AxhkYwAgrplanes> getAllAgrplanes(String fId) {
        List<Integer> serialNumbers = afPartMapper.getAId(fId); //获取到所有设备id
        List<AxhkYwAgrplanes> axhkYwAgrplanes = null;
        if (null != serialNumbers) {
            axhkYwAgrplanes = getByAxhkYwAgrplanes(serialNumbers); //获取到的所有群信息
        } else {
            return null;
        }
        return axhkYwAgrplanes;
    }

    /*
     * 移除设备
     * String uId
     * String fId
     * String serialNumber
     * */
    @Override
    public String getDeleteByFacility(String uId, String fId, Integer serialNumber) {
        String msg = null;
        if ((null != uId && "" != uId) && (null != fId && "" != fId)) {
            Thirdparty thirdparty1 = thirdpartyMapper.getByUser(fId, uId); //判断该用户在该群中是否是队长的 存在
            Integer tag = thirdparty1.getTag();
            if (tag == 1) {
                if ((null != fId && "" != fId) && (null != serialNumber && 0 != serialNumber)) {
                    String userDeparts = "无";
                    String userName = "无";
                    String userId = null;
                    int connStatus = 1;
                    int a = axhkYwAgrplaneMapper.getUpdatePlane3(serialNumber, userDeparts, userName, userId,connStatus);
                    if (a > 0) {
                        int i = afPartMapper.getDeleteAFPart(fId, serialNumber);
                        if (i > 0) {
                            msg = "移除成功!"; //添加成功!
                        } else {
                            msg = "C"; //已经移除掉
                        }
                    }
                } else {
                    msg = "A"; //请正确选择设备!
                }
            } else {
                msg = "B"; //你不是队长没有权限移除队员!
            }
        }
        return msg;
    }

    /*
     * 查看该群中所有的信息  成员  设备  任务
     * String fId
     * */
    @Override
    public List<Fluck> getByFluck(String uId) {
        List<User> userList = null; //用户参数
        List<Fluck> fluck = new LinkedList<>();
        List<AxhkYwAgrplanes> axhkYwAgrplaneList = null; //设备参数
        List<AxhkWorkManagements> axhkWorkManagements = null; //任务参数
        List<String> fIds = thirdpartyMapper.getFluckId(uId); // 该用户所在的所有群组
        for (int i = 0; i < fIds.size(); i++) {
            Fluck flucks = fluckMapper.getFluck(fIds.get(i)); //通过群id查询到的该群信息
            if (null != flucks) {
                List<Integer> aId = afPartMapper.getAId(fIds.get(i)); // 获取该群所有设备 id
                List<Thirdparty> thirdparty = thirdpartyMapper.getUId(fIds.get(i));// 获取该群所有用户 id
                if (null != aId) { //  当群里有设备时
                    axhkYwAgrplaneList = getByAxhkYwAgrplanes(aId);
                    flucks.setAxhkYwAgrplanes(axhkYwAgrplaneList);
                } else { //  当群里没有设备时
                    axhkYwAgrplaneList = null;
                    flucks.setAxhkYwAgrplanes(axhkYwAgrplaneList);
                }
                if (null != thirdparty) { // 当群里有队员时
                    userList = getByUsers(thirdparty);
                    flucks.setUsers(userList);
                } else { // 当群里没有队员时
                    userList = null;
                    flucks.setUsers(userList);
                }
                if (null != userList) { //任务列表
                    for (int j = 0; j < userList.size(); j++) {
                        String tag = userList.get(j).getTag(); //队长标识符
                        if (tag.equals("队长")) {
                            flucks.setCaptainId(userList.get(j).getId());
                            String fluckId = flucks.getId(); // 群id
                            axhkWorkManagements = getByAxhkWorkManagements(fluckId);
                            flucks.setAxhkWorkManagements(axhkWorkManagements);
                        }
                        if (userList.get(j).getId().equals(uId)) { //此时
                            if (tag.equals("队长")) {
                                flucks.setTag("队长");
                                flucks.setTagB(true);
                            } else {
                                flucks.setTag("队员");
                                flucks.setTagB(false);
                            }
                        }
                    }
                } else {
                    axhkWorkManagements = null;
                    flucks.setAxhkWorkManagements(axhkWorkManagements);
                }
            }
            fluck.add(flucks);
        }
        //对axhkWorkManagements按时间进行排序
        Collections.sort(fluck, new Comparator<Fluck>() {
            @Override
            public int compare(Fluck o1, Fluck o2) {
                // 升序
                //return o1.getCreatTime().compareTo(o2.getCreatTime());
                // 降序
                return o2.getCreatTime().compareTo(o1.getCreatTime());
            }
        });
        return fluck;
    }

    /*
     * 获取该群里所有群成员
     * String fId
     * */
    @Override
    public List<User> getByUser(String fId) {
        List<User> userList = new ArrayList<>(); //创建一个 用户集合
        if (null != fId && "" != fId) {
            List<Thirdparty> thirdparty = thirdpartyMapper.getUId(fId);
            for (int i = 0; i < thirdparty.size(); i++) { //遍历群中 所有用户 id
                String tag = null;
                String uIds = thirdparty.get(i).getUId(); //取出每一个 用户id
                Integer tags = thirdparty.get(i).getTag(); //获取到每一个队员标识符
                switch (tags) {
                    case 1:
                        tag = "队长";
                        break;
                    case 0:
                        tag = "队员";
                        break;
                }
                AUser ausers = aUserMapper.getAllAUser(uIds); //调用获取用户方法 获取用户
                if (null != ausers) { //当用户列表不为空时
                    String sexs = null;
                    String id = ausers.getId();//用户id
                    String username = ausers.getUsername();//用户注册名
                    String realname = ausers.getRealname(); //用户注册真实姓名
                    Integer sex = ausers.getSex();//用户性别
                    if (null != sex) {
                        if (sex == 1) {
                            sexs = "男";
                        } else {
                            sexs = "女";
                        }
                    } else {
                        sexs = null;
                    }
                    String email = ausers.getEmail();//用户邮箱
                    String phone = ausers.getPhone(); //用户电话号码

                    User user = new User(id, username, realname, sexs, email, phone, tag);
                    if (null != user) { // 当用户不为空时
                        userList.add(user); //给集合中添加
                    } else {// 当用户为空时
                        userList = null;
                    }
                } else { //当用户列表为空时
                    userList = null;
                }
            }
        } else {
            userList = null;
        }
        if (null != userList) {
            for (int j = 0; j < userList.size(); j++) {
                User user = userList.get(j);
                if (user.getTag().equals("队长")) {
                    int index = userList.indexOf(user); //获取队长在 用户集合中的下标
                    Collections.swap(userList, index, 0);
                }
            }
        }
        return userList;
    }

    /*
     * 删除群
     * String uId
     * String fId
     * */
    @Override
    public String getByDeleteFluck(String uId, String fId) {
        String msg = null;
        if (null != uId && "" != uId) {
            Thirdparty thirdparty1 = thirdpartyMapper.getByUser(fId, uId); //判断该用户在该群中是否是队长
            Integer tag = thirdparty1.getTag();
            if (tag == 1) { //是队长
                List<Integer> number = afPartMapper.getAId(fId); //查询该群下的是否存在设备、并移除  设备序列号
                Fluck fluck = fluckMapper.getFluck(fId);
                String fName = fluck.getFName();
                if (number.size() == 0) { //该群下没有设备
                    int i = fluckMapper.getDeleteFluck(fId); //删除群
                    if (i > 0) {
                        int a = thirdpartyMapper.getDeleteThirdparty(fId); //删除群成员
                        if (a > 0) {
                            msg = "删除成功!";
                        }
                    }
                } else { //群下有设备
                    String userDeparts = null;//群名
                    String userName = "无";// 用户名
                    String userId = null;// 用户id
                    for (int i = 0; i < number.size(); i++) {
                        int serNumber = number.get(i);//每个设备序列号
                       int connStatus = 1;
                        int i2 = axhkYwAgrplaneMapper.getUpdatePlane3(serNumber, userDeparts, userName, userId,connStatus); //修改设备表
                        if (i2 > 0) { //修改成功后
                            fluckMapper.getDeleteFluck(fId); //删除群
                            afPartMapper.getDeleteAFParts(fId);//删除设备
                            int a2 = thirdpartyMapper.getDeleteThirdparty(fId);//删除群成员
                            if (a2 > 0) {
                                msg = "删除成功!";
                            }
                        }
                    }
                }
            } else { //不是队长
                msg = "A";
            }
        }
        return msg;
    }

    /*
     * 通过群获取该群的 成员数量 任务数量 作业总面积
     * String fId
     * */
    @Override
    public Quantity getByQuantity(String fId) {
        Fluck fluck = fluckMapper.getFluck(fId);
        Integer UNum = fluck.getUNum(); //获取的群成员数量
        List<AxhkWorkManagement> axhkWorkManagements = axhkWorkManagementMapper.getWorkAndLand(fId);
        Integer WNum = axhkWorkManagements.size();
        ArrayList<AxhkLandManagements> axhkLandManagements = axhkWorkManagementMapper.getByWorkId(fId);
        Double LArea = 0.0;
        for (int i = 0; i < axhkLandManagements.size(); i++) {
            String landArea = axhkLandManagements.get(i).getLandArea();
            double a = Double.parseDouble(landArea);
            LArea = LArea + a;
        }
        Quantity quantity = new Quantity(UNum, WNum, LArea);
        return quantity;
    }

    /*
     * 通过 飞机序列号跟用户id 查看飞机所属
     * Integer serialNumber
     * String uId
     * */
    @Override
    public String getBoolean(Integer serialNumber, String uId) {
        if (null != serialNumber || 0 == serialNumber) {
            AxhkYwAgrplane axhkYwAgrplane = axhkYwAgrplaneMapper.getAllAxhkYwAgrplane(serialNumber);
            if (null != axhkYwAgrplane) {
                String userId = axhkYwAgrplane.getUserId();
                if (null != userId) { //设备没有被队长移除
                    if (userId.equals(uId)) {
                        int astate = 1;
                        int conn_status = 3;
                        axhkYwAgrplaneMapper.getUpdateAstate(serialNumber, astate,conn_status);
                        return "是该用户所持有！"; //是该用户所持有！
                    } else {//设备已经被队长移除
                        return "C"; //不是该用户持有！
                    }
                } else { ////设备已经被队长移除
                    return "D";//该设备未有持有人
                }
            } else {
                return "B"; // 未查到该设备!
            }
        } else {
            return "A"; // 序列号为空！
        }
    }

    /*
     * 手机App断开连接后 修改连接状态
     * Integer serialNumber
     * */
    @Override
    public String getUpdateAstate(Integer serialNumber) {
        int astate = 0;
        int connStatus = 2;
        if (null != serialNumber || 0 != serialNumber) {

            int i = axhkYwAgrplaneMapper.getUpdateAstate(serialNumber, astate,connStatus);
            if (i > 0) {
                return "未连接状态！";
            } else {
                return "B";
            }
        } else {
            return "C";
        }
    }

    /*
     * 根据 用户id 获取用户集合的方法
     * List<String> uId
     * */
    public List<User> getByUsers(List<Thirdparty> thirdparty) {
        List<User> userList = new ArrayList<>(); //创建一个 用户集合
        for (int i = 0; i < thirdparty.size(); i++) { //遍历群中 所有用户 id
            String tag = null;
            String uIds = thirdparty.get(i).getUId(); //取出每一个 用户id
            Integer tags = thirdparty.get(i).getTag(); //获取到每一个队员标识符
            switch (tags) {
                case 1:
                    tag = "队长";
                    break;
                case 0:
                    tag = "队员";
                    break;
            }
            AUser ausers = aUserMapper.getAllAUser(uIds); //调用获取用户方法 获取用户
            if (null != ausers) { //当用户列表不为空时
                String sexs = null;
                String id = ausers.getId();//用户id
                String username = ausers.getUsername();//用户注册名
                String realname = ausers.getRealname();//用户注册姓名
                Integer sex = ausers.getSex();//用户性别
                if (null != sex) {
                    if (sex == 1) {
                        sexs = "男";
                    } else {
                        sexs = "女";
                    }
                } else {
                    sexs = null;
                }
                String email = ausers.getEmail();//用户邮箱
                String phone = ausers.getPhone(); //用户电话号码
                User user = new User(id, username, realname, sexs, email, phone, tag);
                if (null != user) { // 当用户不为空时
                    userList.add(user); //给集合中添加
                } else {// 当用户为空时
                    userList = null;
                }
            } else { //当用户列表为空时
                userList = null;
            }
        }
        for (int j = 0; j < userList.size(); j++) {
            User user = userList.get(j);
            if (user.getTag().equals("队长")) {
                int index = userList.indexOf(user); //获取队长在 用户集合中的下标
                Collections.swap(userList, index, 0);
            }
        }
        return userList;
    }

    /*
     * 根据 设备序列号serialNumber 获取设备集合的方法
     * List<String> aId
     * */
    public List<AxhkYwAgrplanes> getByAxhkYwAgrplanes(List<Integer> Serialnumber) {
        List<AxhkYwAgrplanes> axhkYwAgrplaneList = new ArrayList<>(); //创建设备集合
        boolean Auser = false;
        for (int i = 0; i < Serialnumber.size(); i++) { //遍历每一个设备序列号serialNumber
            int serialNumbers = Serialnumber.get(i); //从中取出每一个设备序列号serialNumber
            AxhkYwAgrplane axhkYwAgrplane = axhkYwAgrplaneMapper.getAllAxhkYwAgrplane(serialNumbers);// 调用方法获取设备
            if (null != axhkYwAgrplane) { // 当设备不为空时
                String agrPlaneModel = null;
                String activationState = null;
                String salesStatus = null;
                String id = axhkYwAgrplane.getId();
                int serialNumber = axhkYwAgrplane.getSerialNumber();
                String sign = axhkYwAgrplane.getSign();
                String model = axhkYwAgrplane.getModel();
                String agrPlaneModels = axhkYwAgrplane.getAgrPlaneModel();
                String Uid = axhkYwAgrplane.getUserId();
                String userName = axhkYwAgrplane.getUserName();
                int astate = axhkYwAgrplane.getAstate();
                switch (agrPlaneModels) {
                    case "1":
                        agrPlaneModel = "植保机";
                        break;
                    case "2":
                        agrPlaneModel = "行业机";
                        break;
                    case "3":
                        agrPlaneModel = "配件";
                        break;
                }
                Double totalArea = axhkYwAgrplane.getTotalArea();
                String realSign = axhkYwAgrplane.getRealSign();
                String operator = axhkYwAgrplane.getOperator();
                String activationStates = axhkYwAgrplane.getActivationState();
                switch (activationStates) {
                    case "1":
                        activationState = "已激活";
                        break;
                    case "2":
                        activationState = "未激活";
                        break;
                    case "3":
                        activationState = "调试";
                        break;
                }
                String salesStatu = axhkYwAgrplane.getSalesStatus();
                switch (salesStatu) {
                    case "1":
                        salesStatus = "已售";
                        break;
                    case "2":
                        salesStatus = "自用";
                        break;
                }
                String userId = axhkYwAgrplane.getUserName();
                if (userId.equals("无")) {
                    Auser = false;
                } else {
                    Auser = true;
                }

          int connStatus = axhkYwAgrplane.getConnStatus();


//                switch (axhkYwAgrplane.getConnStatus()) {
//                    case 1:
//                        break;
//                    case 2:
//                        break;
//                    case 3:
//                        break;
//                }

                AxhkYwAgrplanes axhkYwAgrplanes = new AxhkYwAgrplanes(id, serialNumber, sign, model, agrPlaneModel, totalArea, realSign, operator, activationState, salesStatus, Auser, Uid, userName, astate,connStatus);
                if (null != axhkYwAgrplanes) {
                    axhkYwAgrplaneList.add(axhkYwAgrplanes);//对设备进行添加
                }
            } else { // 当设备为空时
                axhkYwAgrplaneList = null;
            }
        }
        return axhkYwAgrplaneList;
    }

    /*
     * 根据群id获得该群下所有的地块信息  返回给手机APP
     * String create_by
     * */
    public List<AxhkWorkManagements> getByAxhkWorkManagements(String fluckId) {
        List<AxhkWorkManagement> axhkWorkManagements = axhkWorkManagementMapper.getWorkAndLand(fluckId);
        List<AxhkWorkManagements> axhkWorkManagements1 = new ArrayList<>();
        for (AxhkWorkManagement WorkManagement : axhkWorkManagements) {
            String massifName = null;
            String names = WorkManagement.getLandName();//地块名称
            if (null != names) {
                boolean a = StrUtil.containsBlank(names);
                if (a == true) {
                    massifName = names.trim();
                } else {
                    massifName = names;
                }
            }
            String id = WorkManagement.getId();// 主键
            String createBy = WorkManagement.getCreateBy(); //创建人
            String taskName = WorkManagement.getTaskName();// 任务名称
            String area = WorkManagement.getArea();//地区
            Date startWork = WorkManagement.getStartWork();//任务创建时间
            String crop = WorkManagement.getCrop();//作物
            String jopType = WorkManagement.getJopType(); //作业类型
            String preventTarget = WorkManagement.getPreventTarget();//防止目标
            AxhkWorkManagements axhkWorkManagements2 = new AxhkWorkManagements(id, createBy, taskName, area, startWork, crop, jopType, preventTarget, massifName);
            axhkWorkManagements1.add(axhkWorkManagements2);
        }
        return axhkWorkManagements1;
    }


}
